﻿@using KRM.Core

@using KRM.Core.Library
@{
    ViewBag.Title = "Payment History";
    Layout = "~/Views/Shared/_PopupLayout.cshtml";

    var db = new KRMEntities();
    string orderref = ViewBag.OrderRef;

    var commideaTransactions = db.vKMSCommideaTransactions.Where(a => a.orderref == orderref).ToList();
    var orderRefund = db.tblKRMOrderRefunds.Where(a => a.OrderRef == orderref);
    var paymentTransaction = db.tblKRMPaymentTransactions.Where(a => a.orderref == orderref).OrderByDescending(a => a.paymentTransactionID).FirstOrDefault();

    var cbCardDetail = db.vWebLoadCardDetails.Where(a => a.Orderref == orderref);
    var cardDetail = cbCardDetail.FirstOrDefault(a => a.CCNumber != "");
    var first = true;
    var previous = "";
    var archived = true;
    var idx = 0;
    decimal totalRefunded = orderRefund.Where(p => p.NonRefundAmount != null).Sum(c => c.NonRefundAmount) ??0
                            + orderRefund.Where(p => p.RefundAmount != null).Sum(c => c.RefundAmount) ??0;
    decimal totalTaken = commideaTransactions.Where(p => p.TransactionType == "SALE" && p.PaymentStatus == "SUCCESS").Sum(c => c.Amount) ?? 0 ;

    ViewBag.RefundNonTypeID = new SelectList(db.tblKRMOrderRefundTypes.Where(a => a.Type == false || a.Type == null).OrderBy(b => b.RefundName), "RefundTypeID", "RefundName");
    ViewBag.RefundTypeID = new SelectList(db.tblKRMOrderRefundTypes.Where(a => a.Type == true || a.Type == null).OrderBy(b => b.RefundName), "RefundTypeID", "RefundName");
    var three = paymentTransaction != null ? paymentTransaction.liabilityShift.ToString() : "NO";
}

<div class="padding10">
    @if (Request["msg"] != null && Request["msg"] == "success")
            {
                <div class="alert alert-success">
                    <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>Success.</strong>
                </div>
            }
            else if (Request["msg"] != null)
            {
                <div class="alert alert-error">
                    <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>Error.</strong> @Request["msg"]
                </div>
            }
    <fieldset class="margin0 OrderPayment" style="background: rgb(253, 238, 215); padding: 0 10px;">
        <legend style="background: white">Order Payment</legend>
        @foreach (var item in commideaTransactions)
        {
            if (item.TransactionType == "SALE")
            {
                if ( item.PaymentStatus == "SUCCESS" ) 
                {
                    if ( first ) 
                    {
                        first = false;
                        archived = item.livetable == 0;
                    }
                }

                if (item.livetable == 1)
                {
                    var commidea = db.tblKRWCommideaTransactions.FirstOrDefault(a => a.TransactionID == item.TransactionID);
                    <div class="row-fluid">
                        <div class="span4">
                            <div class="row-fluid">
                                <div class="span5">
                                    Payment status:
                                </div>
                                <div class="span7">
                                    <span class="payment-history-status">@commidea.PaymentStatus</span>
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="span5">
                                    Date:
                                </div>
                                <div class="span7">
                                    @string.Format("{0:dd/MM/yyyy}", commidea.DateofTrans)
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="span5">
                                    User:
                                </div>
                                <div class="span7">
                                    @tblUser.GetUserName(commidea.UserUpdated ?? 0)
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="span5">
                                    3D-Secure:
                                </div>
                                <div class="span7">
                                    @three
                                </div>
                            </div>
                        </div>
                        <div class="span4">
                            <textarea class="input-block-level" rows="3">@commidea.Message</textarea>
                            TransactionID: @commidea.TransactionID
                        </div>
                        <div class="span4">
                            <p class="text-right">@commidea.OrderType</p>
                            <p><a href="mailto:@(commidea.Email)">@commidea.Email</a></p>

                            <div class="row-fluid">
                                <div class="span4">SALE amount:</div>
                                <div class="span8">
                                    <input type="text" class="input-block-level" value="@string.Format("{0:C}", commidea.Amount)" />
                                </div>
                            </div>
                        </div>
                    </div>
                }
                else
                {
                    var commArchives = db.tblKRWCommArchives.FirstOrDefault(a => a.TransactionID == item.TransactionID);
                    <div class="row-fluid">
                        <div class="span4">
                            <div class="row-fluid">
                                <div class="span5">
                                    Payment status:
                                </div>
                                <div class="span7">
                                    <span class="payment-history-status">@commArchives.PaymentStatus</span>
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="span5">
                                    Date:
                                </div>
                                <div class="span7">
                                    @commArchives.DateofTrans
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="span5">
                                    User:
                                </div>
                                <div class="span7">
                                    @tblUser.GetUserName(commArchives.UserUpdated ?? 0)
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="span5">
                                    3D-Secure:
                                </div>
                                <div class="span7">
                                    @{
                                        
                                    }
                                </div>
                            </div>
                        </div>
                        <div class="span4">
                            <textarea class="input-block-level" rows="3">@commArchives.Message</textarea>
                            TransactionID: @commArchives.TransactionID
                        </div>
                        <div class="span4">
                            <p class="text-right">@commArchives.OrderType</p>
                            <p><a href="mailto:@(commArchives.Email)">@commArchives.Email</a></p>
                            <div class="row-fluid">
                                <div class="span4">SALE amount:</div>
                                <div class="span8">
                                    <input type="text" class="input-block-level" value="@string.Format("{0:C}", commArchives.Amount)" />
                                </div>
                            </div>
                        </div>
                    </div>
                }

            }
        }

    </fieldset>
    @*<ul class="tabs tabs-inline tabs-top orderdetail-task-tabs">
        <li class="active">
            <a href="#Standard" data-toggle="tab">Standard refund</a>
        </li>
        <li>
            <a href="#Non" data-toggle="tab">Non refund type</a>
        </li>
        <li>
            <a href="#Other" data-toggle="tab">Other refund type</a>
        </li>
    </ul>

    <div class="tab-content padding tab-content-inline tab-content-bottom orderdetail-task-tabs-content" style="background: MistyRose">
        <div class="tab-pane active" id="Standard">
            <form action="#" method="POST" class="margin0">
                <input type="hidden" name="totalRefunded" value="@totalRefunded"/>
                <input type="hidden" name="totalTaken" value="@totalTaken"/>
                <input type="hidden" name="archived" value="@archived.ToString()"/>
                <input type="hidden" name="orderref" value="@orderref"/>
                <div class="row-fluid">
                    <div class="span5">
                        <div class="row-fluid">
                            <div class="span3 text-right">Cart to refund</div>
                            <div class="span9">
                                <select class="input-block-level" name="transactionId" id="cbTransactionId" required="required">
                                    @foreach (var item in cbCardDetail)
                                    {
                                        string cardnum;
                                        string cardstring;
                                        string cardnumunencripted = item.CCNumber;
                                        if (cardnumunencripted == "DELETED" || cardnumunencripted == "")
                                        {
                                            cardnum = cardnumunencripted;
                                            cardstring = cardnum;
                                        }
                                        else
                                        {
                                            cardnum = Encryption.DecryptText(item.CCNumber);
                                            cardstring = "xxxxxxxxxxxx" + cardnum.Substring(cardnum.Length - 4, 4);
                                        }
                                        idx++;
                                        var current = item.CCNumber + item.ExpiryDate;
                                        <option value="@item.TransactionID">@idx) @item.CardScheme &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @cardstring</option>
                                    }
                                </select>
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span3 text-right">Expiry</div>
                            <div class="span3">
                                <input type="text" class="input-block-level" disabled="disabled" value="@cardDetail.ExpiryDate" />
                            </div>
                            <div class="span3">Issue num</div>
                            <div class="span3">
                                <input type="text" class="input-block-level" disabled="disabled" value="@cardDetail.IssueNum" />
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span3 text-right">Issue date</div>
                            <div class="span3">
                                <input type="text" class="input-block-level" disabled="disabled" value="@cardDetail.IssueDate" />
                            </div>
                            <div class="span5">
                                Use new card
                                <input type="checkbox" id="cbnewcard" name="cbNewCard" />
                            </div>
                        </div>
                    </div>
                    <fieldset class="span7 margin0">
                        <legend>New Card</legend>
                        <div class="row-fluid">
                            <div class="span4">Card Number</div>
                            <div class="span2">Expiry</div>
                            <div class="span2">Issue Date</div>
                            <div class="span2">Issue</div>
                            <div class="span2">CV2</div>
                        </div>
                        <div class="row-fluid">
                            <div class="span4">
                                <input type="text" class="input-block-level cardnumber" disabled="disabled" name="NewCardNum" onblur="validCardNum(this.value);" />
                            </div>
                            <div class="span2">
                                <input type="text" class="input-block-level expiry" disabled="disabled" name="NewExpiryDate" />
                            </div>
                            <div class="span2">
                                <input type="text" class="input-block-level issuedate" disabled="disabled" name="NewIssueDate" />
                            </div>
                            <div class="span2">
                                <input type="text" class="input-block-level issue" disabled="disabled" name="NewIssueNum" />
                            </div>
                            <div class="span2">
                                <input type="text" class="input-block-level cv2" disabled="disabled" name="NewCV2" />
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="row-fluid text-right">
                    <div class="span6"></div>
                    <div class="span2">Standard Refund</div>
                    <div class="span2">
                        <input type="text" class="input-block-level" value="@string.Format("{0:N}", (totalTaken - totalRefunded) < 0 ? 0 : (totalTaken - totalRefunded))" name="refundAmount" />
                    </div>
                    <div class="span2">
                        <button class="btn btn-primary input-block-level" type="submit" onclick="javascript:return confirm('Are you sure you want to give a refund?');">Give Refund</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="tab-pane" id="Non">
            <form action="/orderview/nonrefundtype" method="POST" class="margin0">
                <input type="hidden" name="orderref" value="@orderref"/>
                <div class="row-fluid">
                    <div class="span4">
                        <p>Extra information</p>
                        <textarea class="input-block-level" rows="3" name="ExtraInfo"></textarea>
                    </div>
                    <fieldset class="span4" style="margin-top: 21px;">
                        <legend>Select Non Refund Type</legend>
                        @Html.DropDownList("RefundNonTypeID", null, new {@class="input-block-level"})
                    
                    </fieldset>
                </div>
                <div class="row-fluid text-right">
                    <div class="span3">Amount None Refunded</div>
                    <div class="span2">
                        <input type="text" class="input-block-level" name="NonRefundAmount" value="@string.Format("{0:N}", (totalTaken - totalRefunded) < 0 ? 0 : (totalTaken - totalRefunded))" />
                    </div>
                    <div class="span3">
                        <button class="btn btn-primary input-block-level">Mark as Non Refunded</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="tab-pane" id="Other">
            <form action="/orderview/otherrefundtype" method="POST" class="margin0">
                <input type="hidden" name="orderref" value="@orderref"/>
                <div class="row-fluid">
                    <div class="span4">
                        <p>Extra information</p>
                        <textarea class="input-block-level" rows="3" name="ExtraInfo"></textarea>
                    </div>
                    <fieldset class="span4" style="margin-top: 21px;">
                        <legend>Select Refund Type</legend>
                        @Html.DropDownList("RefundTypeID", null, new {@class="input-block-level"})
                    </fieldset>
                </div>
                <div class="row-fluid text-right">
                    <div class="span3">Amount Refunded</div>
                    <div class="span2">
                        <input type="text" class="input-block-level" name="RefundAmount" value="@string.Format("{0:N}", (totalTaken - totalRefunded) < 0 ? 0 : (totalTaken - totalRefunded))" />
                    </div>
                    <div class="span3">
                        <button class="btn btn-primary input-block-level">Mark as Refunded</button>
                    </div>
                </div>
            </form>
        </div>
    </div>*@
    <fieldset style="background: MistyRose; /*height: 300px; overflow: auto*/">
        <legend style="background: white">Non Refund/Refund History</legend>
        @foreach (var item in commideaTransactions.Where(c => c.TransactionType != "SALE"))
        {
            if (item.livetable == 1)
            {
                var commidea = db.tblKRWCommideaTransactions.FirstOrDefault(a => a.TransactionID == item.TransactionID);
                <div class="row-fluid">
                    <div class="span4">
                        <div class="row-fluid">
                            <div class="span5">
                                Date:
                            </div>
                            <div class="span7">
                                @string.Format("{0:dd/MM/yyyy}", commidea.DateofTrans)
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span5">
                                User:
                            </div>
                            <div class="span7">
                                @tblUser.GetUserName(commidea.UserUpdated ?? 0)
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span5">
                                3D-Secure:
                            </div>
                            <div class="span7">
                                @three
                            </div>
                        </div>
                    </div>
                    <div class="span4">
                        <textarea class="input-block-level" rows="3">@commidea.Message</textarea>
                        TransactionID: @commidea.TransactionID
                    </div>
                    <div class="span4">
                        <p class="text-right">@commidea.OrderType</p>
                        <p><a href="mailto:@commidea.Email">@commidea.Email</a></p>

                        <div class="row-fluid">
                            <div class="span4">SALE amount:</div>
                            <div class="span8">
                                <input type="text" class="input-block-level" value="@string.Format("{0:C}", commidea.Amount)" />
                            </div>
                        </div>
                    </div>
                </div>
            }
            else
            {
                var commArchives = db.tblKRWCommArchives.FirstOrDefault(a => a.TransactionID == item.TransactionID);
                <div class="row-fluid">
                    <div class="span4">
                        <div class="row-fluid">
                            <div class="span5">
                                Date:
                            </div>
                            <div class="span7">
                                @commArchives.DateofTrans
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span5">
                                User:
                            </div>
                            <div class="span7">
                                @tblUser.GetUserName(commArchives.UserUpdated ?? 0)
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span5">
                                3D-Secure:
                            </div>
                            <div class="span7">
                                @three
                            </div>
                        </div>
                    </div>
                    <div class="span4">
                        <textarea class="input-block-level" rows="3">@commArchives.Message</textarea>
                        TransactionID: @commArchives.TransactionID
                    </div>
                    <div class="span4">
                        <p class="text-right">@commArchives.OrderType</p>
                        <p><a href="mailto:@commArchives.Email">@commArchives.Email</a></p>
                        <div class="row-fluid">
                            <div class="span4">SALE amount:</div>
                            <div class="span8">
                                <input type="text" class="input-block-level" value="@string.Format("{0:C}", commArchives.Amount)" />
                            </div>
                        </div>
                    </div>
                </div>
            }
        }
        @foreach (var item in orderRefund)
        {
            var refundHistory = db.vWebRefundHistories.FirstOrDefault(a => a.OrderRefundID == item.OrderRefundID);
                <div class="row-fluid" style="border: 1px solid #ccc; padding: 5px 0 0 5px; @(!string.IsNullOrEmpty(refundHistory.NonRefundAmount.ToString()) ? "background:YellowGreen" : "background:rgb(253, 238, 215)")">
                    <div class="span4">
                        <div class="row-fluid">
                            <div class="span5">
                                Date:
                            </div>
                            <div class="span7">
                                @refundHistory.RefundDate
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span5">
                                User:
                            </div>
                            <div class="span7">
                                @tblUser.GetUserName(refundHistory.RefundByUserId ?? 0)
                            </div>
                        </div>
                        <div class="row-fluid">
                            <div class="span5">
                                3D-Secure:
                            </div>
                            <div class="span7">
                                @three
                            </div>
                        </div>
                    </div>
                    <div class="span4">
                        <textarea class="input-block-level" rows="2">@refundHistory.ExtraInfo</textarea>
                        Order refund ID: @refundHistory.OrderRefundID
                    </div>
                    <div class="span4">
                        <h4 style="color: red">@refundHistory.RefundName</h4>
                        <div class="row-fluid">
                            <div class="span7">@(!string.IsNullOrEmpty(refundHistory.NonRefundAmount.ToString()) ? "Non Refunded amount:" : "Refunded amount:")</div>
                            <div class="span4">
                                <input type="text" class="input-block-level" value="@(!string.IsNullOrEmpty(refundHistory.NonRefundAmount.ToString()) ? string.Format("{0:N}", refundHistory.NonRefundAmount) : string.Format("{0:N}", refundHistory.RefundAmount))" />
                            </div>
                        </div>
                    </div>
                </div>
            }
    </fieldset>
</div>

